package com.mf.pte.techique;

import com.mf.pte.pojo.Item;
import com.mf.pte.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class Procedure {
    private String procedurePurpose;

    private final List<ProcedureStep> steps = new ArrayList<>();

    public Procedure(){}

    public void addProcedureStep(ProcedureStep step){
        steps.add(step);
    }

    public void print(int level){
        System.out.println(StringUtil.getXTable(level).append(procedurePurpose));
        for (ProcedureStep step : steps) {
            step.print(level+1);
        }
    }

    public Procedure generateComponentAssembly(Item component){
        procedurePurpose = "部装:"+component.getComponent().getName();
        int sequence = 0;
        for(Item child:component.getChildren()){
            if(child.isAsm()&&!child.isUsed()){
                sequence+=10;
                String stepName = sequence+"序组装:"+child.getComponent().getName();
                List<Item> stepMaterials = child.getChildren();
                addProcedureStep(new ProcedureStep(stepName,stepMaterials));
                child.useItsChildren();
            }
        }
        String stepName = (sequence+10)+ "组装其他件";
        addProcedureStep(new ProcedureStep(stepName,component.getChildren()));
        component.useItsChildren();
        return this;
    }

    public Procedure generateUnitedAssembly(Item component){
        procedurePurpose = "总装:"+ component.getComponent().getName();
        int sequence = 0;
        for(Item child:component.getChildren()){
            if(child.isAsm()){
                sequence+=10;
                String stepName = sequence+"装配:"+child.getComponent().getName();
                ProcedureStep procedureStep = new ProcedureStep(stepName);
                procedureStep.addItemInThisStep(child);
                addProcedureStep(procedureStep);
                child.used();
            }
        }
        String stepName =  (sequence+10)+"装配其他件";
        ArrayList<Item> lastComponents = new ArrayList<>();
        for(Item child:component.getChildren()){
            if(!child.isUsed()){
                lastComponents.add(child);
            }
        }
        addProcedureStep(new ProcedureStep(stepName,lastComponents));
        component.useItsChildren();
        return this;
    }

}
